<?php

/***

LZRoster: A roster for World of Warcraft.
Copyright (C) 2007  Emmanuel Cron

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

***/

require_once "class/Load.class.php";
require_once "class/Config.class.php";
require_once "class/Inputs.class.php";
require_once "class/Tooltip.class.php";
require_once "class/StatusRoster.class.php";
require_once "class/SessionRoster.class.php";
require_once "class/Localization.class.php";
require_once "class/ObjectContainer.class.php";
require_once "class/WowTable.class.php";
require_once "class/SubscriptionManager.class.php";
require_once "class/SubscriptionDisplayer.class.php";
require_once "class/CharacterDisplayer.class.php";

// For loading information in footer
Load::GetInstance();

// Session initialization
SessionRoster::GetInstance();

if ( SessionRoster::GetInstance()->IsAdminConnected() == false ) {
	SessionRoster::GetInstance()->Redirect("admin_connection.php");
}

if ( Inputs::GetInstance()->HasPostData('a_chg_nickname') ) {
	
	if ( Inputs::GetInstance()->Post('new_nickname') == null ) {
		StatusRoster::GetInstance()->AddError( Localization::GetInstance()->Get('you must enter a nickname') );
	} elseif ( SubscriptionManager::GetInstance()->HasNickname(Inputs::GetInstance()->Post('new_nickname')) == SUBSCRIPTION_MANAGER_NICKNAME_EXISTS ) {
		StatusRoster::GetInstance()->AddError( Localization::GetInstance()->Get('this nickname already exists') );
	} elseif ( SubscriptionManager::GetInstance()->HasName(Inputs::GetInstance()->Post('name')) == SUBSCRIPTION_MANAGER_DOES_NOT_EXIST ) {
		StatusRoster::GetInstance()->AddError( Localization::GetInstance()->Get('this account does not exist') );
	} elseif ( SubscriptionManager::GetInstance()->GetSubscription( SubscriptionManager::GetInstance()->GetAccountIdByName(Inputs::GetInstance()->Post('name')) )->GetName() == strtolower(Inputs::GetInstance()->Post('new_nickname')) ) {
		StatusRoster::GetInstance()->AddError( Localization::GetInstance()->Get('nickname cannot be the same as the account name') );
	} elseif ( SubscriptionManager::GetInstance()->UpdateNickname( SubscriptionManager::GetInstance()->GetAccountIdByName(Inputs::GetInstance()->Post('name')), Inputs::GetInstance()->Post('new_nickname')) == false ) {
		StatusRoster::GetInstance()->AddError( Localization::GetInstance()->Get('could not update nickname') );
	} else {
		StatusRoster::GetInstance()->AddSuccess( Localization::GetInstance()->Get('nickname updated successfully for account %s', SubscriptionManager::GetInstance()->GetSubscription(SubscriptionManager::GetInstance()->GetAccountIdByName(Inputs::GetInstance()->Post('name')))->GetName() ) );
	}
	
	SessionRoster::GetInstance()->SaveStatus(StatusRoster::GetInstance());
	SessionRoster::GetInstance()->Redirect("admin.php");
}

if ( Inputs::GetInstance()->HasPostData('a_chg_pwd') ) {
	if ( SubscriptionManager::GetInstance()->CheckPassword(Inputs::GetInstance()->Post('new_pass')) == false ) {
		StatusRoster::GetInstance()->AddError( Localization::GetInstance()->Get('the password is too short') );
	} elseif ( SubscriptionManager::GetInstance()->HasName(Inputs::GetInstance()->Post('name')) == SUBSCRIPTION_MANAGER_DOES_NOT_EXIST ) {
		StatusRoster::GetInstance()->AddError( Localization::GetInstance()->Get('this account does not exist') );
	} elseif ( SubscriptionManager::GetInstance()->SetPassword( SubscriptionManager::GetInstance()->GetAccountIdByName(Inputs::GetInstance()->Post('name')), Inputs::GetInstance()->Post('new_pass')) == false ) {
		StatusRoster::GetInstance()->AddError( Localization::GetInstance()->Get('could not update password') );
	} else {
		StatusRoster::GetInstance()->AddSuccess( Localization::GetInstance()->Get('password updated successfully for account %s', SubscriptionManager::GetInstance()->GetSubscription(SubscriptionManager::GetInstance()->GetAccountIdByName(Inputs::GetInstance()->Post('name')))->GetName() ) );
	}
	
	SessionRoster::GetInstance()->SaveStatus(StatusRoster::GetInstance());
	SessionRoster::GetInstance()->Redirect("admin.php");
}

if ( Inputs::GetInstance()->HasPostData('a_del') ) {
	if ( SubscriptionManager::GetInstance()->HasName(Inputs::GetInstance()->Post('name')) == SUBSCRIPTION_MANAGER_DOES_NOT_EXIST ) {
		StatusRoster::GetInstance()->AddError( Localization::GetInstance()->Get('this account does not exist') );
	} elseif ( SubscriptionManager::GetInstance()->GetAccountIdByName(Inputs::GetInstance()->Post('name')) == SessionRoster::GetInstance()->GetAccountId() ) {
		StatusRoster::GetInstance()->AddError( Localization::GetInstance()->Get('you cannot delete your own account') );
	} elseif ( SubscriptionManager::GetInstance()->Delete( SubscriptionManager::GetInstance()->GetAccountIdByName(Inputs::GetInstance()->Post('name')) ) == false ) {
		StatusRoster::GetInstance()->AddError( Localization::GetInstance()->Get('could not delete account') );
	} else {
		StatusRoster::GetInstance()->AddSuccess( Localization::GetInstance()->Get('account deleted successfully: %s', SubscriptionManager::GetInstance()->GetSubscription(SubscriptionManager::GetInstance()->GetAccountIdByName(Inputs::GetInstance()->Post('name')))->GetName() ) );
	}
	
	SessionRoster::GetInstance()->SaveStatus(StatusRoster::GetInstance());
	SessionRoster::GetInstance()->Redirect("admin.php");
}

if ( Inputs::GetInstance()->HasGetData('c_del') )
{	
	$character = CharacterManager::GetInstance()->GetCharacterById(Inputs::GetInstance()->Get('id'));
	
	if ($character == null) {
		StatusRoster::GetInstance()->AddError(Localization::GetInstance()->Get('this character does not exist'));
	} else if (CharacterManager::GetInstance()->DeleteCharacter($character->GetName()) == false) {
		StatusRoster::GetInstance()->AddError(Localization::GetInstance()->Get('character deletion failed'));
	} else {
		StatusRoster::GetInstance()->AddSuccess(Localization::GetInstance()->Get('character successfully delete'));
	}
	
	SessionRoster::GetInstance()->SaveStatus(StatusRoster::GetInstance());
	SessionRoster::GetInstance()->Redirect("admin.php");
}

$invalid_characters = CharacterManager::GetInstance()->GetCharactersWithInvalidAccount();
if (count($invalid_characters) > 0) {
	$wow_table_invalid_characters = new WowTable( Localization::GetInstance()->Get('characters not linked to any account') );
	$wow_table_invalid_characters->AddSubSection();
	$wow_table_invalid_characters->AddColumn( Localization::GetInstance()->Get('name') );
	$wow_table_invalid_characters->AddColumn( Localization::GetInstance()->Get('delete'), "center" );
	
	foreach($invalid_characters as $character) {
		$wow_table_invalid_characters->AddLine(0, array(
			CharacterDisplayer::GetInstance()->Name($character),
			CharacterDisplayer::GetInstance()->__AdminDelete($character)
		));
	}
}


$wow_table = new WowTable( Localization::GetInstance()->Get('accounts') );
$wow_table->AddSubSection();
$wow_table->AddColumn( Localization::GetInstance()->Get('account') );
$wow_table->AddColumn( Localization::GetInstance()->Get('change nickname'), "center", 250 );
$wow_table->AddColumn( Localization::GetInstance()->Get('change password'), "center", 270 );
$wow_table->AddColumn( Localization::GetInstance()->Get('delete account'), "center", 125 );

foreach (SubscriptionManager::GetInstance()->GetAllAccounts() as $subscription) {
	$wow_table->AddLine(0, array(
		SubscriptionDisplayer::GetInstance()->Name($subscription),
		SubscriptionDisplayer::GetInstance()->__ChangeNickname($subscription),
		SubscriptionDisplayer::GetInstance()->__ChangePassword($subscription),
		SubscriptionDisplayer::GetInstance()->__Delete($subscription)
	));
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr">
	<head>
		<title>LZRoster<?php echo Config::GetInstance()->HasRosterTitle() ?  " - ".Config::GetInstance()->GetRosterTitle() : null; ?></title>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<link rel="stylesheet" media="screen" type="text/css" href="styles/_.css" />
		<link rel="stylesheet" media="screen" type="text/css" href="styles/wowtable.css" />
		<script type="text/javascript" src="js/overlib.js"><!-- overLIB (c) Erik Bosrup --></script>
	</head>
	<body>
		
		<?php require_once "inc/head.php"; ?>
		<?php require_once "inc/hmenu.php"; ?>
		
		<h2><?php echo Localization::GetInstance()->Get('admin'); ?></h2>
		
		<?php
		if (StatusRoster::GetInstance()->HasMessage() or SessionRoster::GetInstance()->HasStatus()) {
		?>
			
			<p>
				<?php echo Localization::GetInstance()->Get('this page produced some messages. please refer to the following lines'); ?>
			</p>
			
			<?php echo SessionRoster::GetInstance()->HasStatus() ? SessionRoster::GetInstance()->GetStatus()->GetStack_InList() : StatusRoster::GetInstance()->GetStack_InList(); ?>
			
			<p>
				<?php echo Localization::GetInstance()->Get('you can refresh the page %s and try again', "admin.php"); ?>
			</p>
			
		<?php
		} else {
		?>
		
		<h3><?php echo Localization::GetInstance()->Get('log files'); ?></h3>
		
		<p>
			<?php echo Localization::GetInstance()->Get('log files can be found in the logs folder of your roster. you can click %s here (...)', "logs/"); ?>
		</p>
		
		<h3><?php echo Localization::GetInstance()->Get('grace period'); ?>: <?php echo Config::GetInstance()->GetGracePeriod()->GetLocalizedName(); ?></h3>
			
		<p>
			<?php echo Localization::GetInstance()->Get('the grace period is the period during which a character is valid on the roster since its last update'); ?>
		</p>
		
		<p>
			<em><?php echo Localization::GetInstance()->Get('in order to change this value, you must run the installation script'); ?></em>
		</p>
		
		<?php
		if (isset($wow_table_invalid_characters)) {
		?>
		
			<h3><?php echo Localization::GetInstance()->Get('purge invalid characters'); ?></h3>
			
			<p>
				<?php echo Localization::GetInstance()->Get('when manually deleting some data into database, there may be characters that are not (...)'); ?>
			</p>
			
			<?php $wow_table_invalid_characters->GetAndPrintTable(); ?>
		
		<?php
		}
		?>
		
		<h3><?php echo Localization::GetInstance()->Get('update/delete account'); ?></h3>
		
		<?php $wow_table->GetAndPrintTable(); ?>
		
		<?php
		}
		?>
		
		<?php require_once "inc/foot.php"; ?>
		
	</body>
</html>